from apps.common.base.views import *
from apps.pay.order.sceneHandle import payReturnScene
from apps.user.dao import UserDao
from apps.pay.recordIncomes.dao import RecordIncomesDao
from apps.pay.order.dao import OrderDao


class IncomePayViewSet(BaseViewSet):

    '''发起积分支付'''
    @action(methods=['post'], detail=False)
    def sendIncomsPay(self, request):
        params = getRequestParams(request)
        if isEmptyStr(params.get("payway")):
            return Response(response_message('参数payway必填'))
        _order = OrderDao().findOneByOrderId(params.get("orderId"))
        if params.get('payway') == 'incomes':
            if request.user.incomes < _order.realPay:
                print('资产不足', request.user.incomes)
                return Response(response_modalMessage('资产不足，请使用其他方式支付'))
            else:
                UserDao().subWallet('incomes', _order.user, _order.realPay)
                data = {}
                data['lessee'] = _order.lessee.id
                data['scene'] = 'expense'
                data['user'] = _order.user.id
                data['fromUser'] = _order.user.id
                data['orderId'] = _order.orderId
                data['money'] = _order.realPay
                data['payway'] = 'incomes'
                data['changeWay'] = 'sub'
                data['orderStatus'] = 'FINISH'
                RecordIncomesDao().create(data)
            payReturnScene('incomes', _order.orderId)
        return Response(response_success('支付成功'))